//
//  TKYSelectedView.m
//  StationHouseHelper
//
//  Created by apple on 2018/5/6.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYSelectedView.h"

@implementation TKYSelectedView

- (instancetype)initWithFrame:(CGRect)frame titleLabelText:(NSString *)titleLabelTex placeholderText:(NSString *)placeholderText imageName:(NSString *)imageStr{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        [self initWithTitleLabelText:titleLabelTex placeholderText:placeholderText imageName:imageStr];
    }
    return self;
}

- (void)initWithTitleLabelText:(NSString *)titleLabelTex placeholderText:(NSString *)placeholderText imageName:(NSString *)imageStr{
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kWidthScale *20,kHeightScale *18, kWidthScale *70, kHeightScale*20)];
    
    self.titleLabel.text = titleLabelTex;
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.titleLabel sizeToFit];
    [self addSubview: self.titleLabel];
    UITextField * textField;
    textField  = [[UITextField alloc]initWithFrame:CGRectMake(kScreenWidth - kWidthScale *215 , kHeightScale *15, kWidthScale*190, kHeightScale*20)];
    //textField.backgroundColor = [UIColor redColor];
    textField.placeholder = placeholderText;
    textField.textColor = [UIColor blackColor];
    [self setRightViewWithTextField:textField imageName:imageStr];

    textField.font = [UIFont systemFontOfSize:14];
    textField.textAlignment = NSTextAlignmentRight;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    self.textField = textField;
    [self addSubview:textField];
    
    self.selectedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.selectedBtn.frame = textField.frame;
    self.selectedBtn.backgroundColor = [UIColor clearColor];
    [self.selectedBtn addTarget:self action:@selector(selectedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.selectedBtn];
    
    UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, kHeightScale *48, kScreenWidth, 0.5)];
    line.backgroundColor = kUIColorFromRGB(0XE5E5E5, 1);
    self.line = line;
    [self addSubview:line];
}

- (void)selectedBtnClick:(UIButton *)button{
    if (self.selectedBtnBlock) {
        self.selectedBtnBlock(button);
    }
}

/**
 给UITextField添加右视图
 */
-(void)setRightViewWithTextField:(UITextField *)textField imageName:(NSString *)imageName{
    UIImageView *rightView = [[UIImageView alloc]init];
    rightView.image = [UIImage imageNamed:imageName];
    rightView.kx_size = CGSizeMake(kWidthScale*11.3, kHeightScale *11.3);
    rightView.contentMode = UIViewContentModeCenter;
    textField.rightView = rightView;
    textField.rightViewMode = UITextFieldViewModeAlways;
}

//计算文本的宽度
- (CGFloat )sizeWidth:(NSString *)text{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    CGFloat widthSize = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.width;
    return widthSize;
}


@end
